/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import com.rh.util.Parser;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author hjones
 */
public class ConversationBuffer extends AbstractXmlBuffer {
    private static void debug(Object o, boolean pTF)  {
        if (pTF)  {
            // System.out.println("ConversationBuffer: " + o);
            return;
        }
        debug(o);
    }
    private static void debug(Object o)  {
        // System.out.println("ConversationBuffer: " + o);
    }
    
    public static enum Mode  {
        PRECONDITIONS, RESPONSES, GAME_EFFECTS;
    }

    private List<ChatKeys> chats = new ArrayList<ChatKeys>();
    public List<ChatKeys> getChats()  { return chats; }
    
    public ConversationBuffer()  {
        super();
        init();
    }
    
    private Mode mode = null;
    
    private String id = "";
    private String text = "";
    private String speakerID = "";
    private boolean intro = false;
    private PreconditionBuffer preconditions = null;
    private ResponseBuffer responses = null;
    private GameEffectsBuffer gameEffects = null;
    
    void init()  {
        mode = null;
        
        id = "";
        text = "";
        speakerID = "";
        intro = false;
        preconditions = new PreconditionBuffer();
        responses = new ResponseBuffer();
        gameEffects = new GameEffectsBuffer();
    }
    
    @Override
    void startElement(String pName)  {
        super.startElement(pName);
        if ("Intro".equals(pName))  {
            intro = true;
        }
        else if ("preconditions".equals(pName))  {
            mode = Mode.PRECONDITIONS;
        }
        else if ("responses".equals(pName))  {
            mode = Mode.RESPONSES;
        }
        else if ("effects".equals(pName))  {
            mode = Mode.GAME_EFFECTS;
        }
        
        if (mode == Mode.PRECONDITIONS)  {
            preconditions.startElement(pName);
        }        
        else if (mode == Mode.RESPONSES)  {
            responses.startElement(pName);
        }
        else if (mode == Mode.GAME_EFFECTS)  {
            gameEffects.startElement(pName);
        }
    }
    
    @Override
    void characters(String pValue) {
        pValue = Parser.trim(pValue);
        if (pValue.length() == 0)  return;
        
        if (mode == Mode.PRECONDITIONS)  {
            preconditions.characters(pValue);
        }        
        else if (mode == Mode.RESPONSES)  {
            responses.characters(pValue);
        }
        else if (mode == Mode.GAME_EFFECTS)  {
            gameEffects.characters(pValue);
        }
        
        debug(getStartElement() + " := " + pValue);
        if (mode == null)  {
            if ("ID".equals(getStartElement()))  {
                id = pValue;
            }
            else if ("Text".equals(getStartElement()))  {
                text = pValue;
            }
            else if ("Speaker".equals(getStartElement()))  {
                speakerID = pValue;
            }
        }
    }
    
    @Override
    void endElement(String pName)  {
        if (mode == Mode.PRECONDITIONS)  {
            preconditions.endElement(pName);
        }        
        else if (mode == Mode.RESPONSES)  {
            responses.endElement(pName);
        }
        else if (mode == Mode.GAME_EFFECTS)  {
            gameEffects.endElement(pName);
        }

        if ("preconditions".equals(pName))  {
            mode = null;
        }
        else if ("effects".equals(pName))  {
            mode = null;
        }
        else if ("responses".equals(pName))  {
            mode = null;
        }
        
        if ("chat".equals(pName))  {
            debug(
                "Add Chat: " + 
                "\n  " + id + ", " + speakerID + ", " + intro +
                "\n  Text: " + text + ", " + 
                "\n  Effects: " + gameEffects.getInitializations() + ", " + 
                "\n  Responses: " + responses.getResponses() + ", " + 
                "\n  Preconditions: " + preconditions.getChecks() + ", " + 
                "", true
            );
            ChatKeys ck = new ChatKeys();
            ck.setID(id);
            ck.setText(text);
            ck.setIntro(intro);
            ck.setSpeakerID(speakerID);
            ck.setGameEffectKeys(gameEffects.getInitializations());
            ck.setResponses(responses.getResponses());
            ck.setConditions(preconditions.getChecks());
            
            chats.add(ck);
            init();
        }
    }
    

}
